#= require jsuri
class window.Spree
  @ready: (callback) ->
    if typeof Turbolinks isnt 'undefined' and Turbolinks.supported
      jQuery(document).on 'turbolinks:load', -> callback(jQuery)
    else
      jQuery(document).ready(callback)

  @mountedAt: ->
    "<%= Rails.application.routes.url_helpers.spree_path(trailing_slash: true) %>"

  @pathFor: (path) ->
    locationOrigin = "#{window.location.protocol}//#{window.location.hostname}" + (if window.location.port then ":#{window.location.port}" else "")
    "#{locationOrigin}#{@mountedAt()}#{path}"

  # Helper function to take a URL and add query parameters to it
  # Uses the JSUri library from here: https://github.com/derek-watson/jsUri
  # Thanks to Jake Moffat for the suggestion: https://twitter.com/jakeonrails/statuses/321776992221544449
  @url: (uri, query) ->
    if Spree.env == 'development' || Spree.env == 'test'
      console.warn 'Spree.url is deprecated, please use Spree.ajax for your request instead.'
    if uri.path == undefined
      uri = new Uri(uri)
    if query
      $.each query, (key, value) ->
        uri.addQueryParam(key, value)
    return uri

  # This function automatically appends the API token
  # for the user to the end of any URL.
  # Immediately after, this string is then passed to jQuery.ajax.
  #
  # Spree.ajax works in two ways to support common jQuery syntax:
  #
  # Spree.ajax("url", {settings: 'go here'})
  # or:
  # Spree.ajax({url: "url", settings: 'go here'})
  #
  # Spree.getJSON has the same method signature as $.getJSON
  @ajax: (url, options) ->
    if typeof(url) == "object"
      options = url
      url = undefined

    options = options || {}

    options = $.extend(options, { headers: { "X-Spree-Token": Spree.api_key } })
    $.ajax(url, options)

  @routes:
    states_search: @pathFor('api/states')
    apply_coupon_code: (order_id) ->
      Spree.pathFor("api/orders/#{order_id}/apply_coupon_code")

  @getJSON: (url, data, success) ->
    if typeof data is 'function'
      success = data
      data = undefined
    @ajax(
      dataType: "json",
      url: url,
      data: data,
      success: success
    )
